package com.totoro.tygeneratedragonblock.commands;

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.totoro.tygeneratedragonblock.TyGenerateDragonBlock;
import net.minecraft.server.v1_12_R1.BlockPosition;
import net.minecraft.server.v1_12_R1.TileEntitySkull;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.lang.reflect.Field;
import java.util.UUID;
import java.util.logging.Logger;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/9 - 22:11
 * @description
 **/
public class TestCommand implements CommandExecutor {

    private static final Logger logger = TyGenerateDragonBlock.getInstance().getLogger();

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Player player = (Player) sender;
        ItemStack item = player.getInventory().getItemInMainHand();
        if(args[0].equalsIgnoreCase("test")) {
            ItemStack head = getHead("自定义方块", "矿物");
            placeCustomHead(player, head);
            player.sendMessage("§a已在你的脚下放置了自定义头颅！");
        }

        return false;
    }

    // 创建自定义头颅ItemStack
    public static ItemStack getHead(String name, String match) {
        ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (short)3);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name);
        try {
            GameProfile profile = new GameProfile(UUID.fromString("014df015-7eba-4ad0-a0e0-83164b7a45f2"), name);
            PropertyMap properties = profile.getProperties();
            properties.put("textures", new Property("textures", match));
            Field profileF = meta.getClass().getDeclaredField("profile");
            profileF.setAccessible(true);
            profileF.set(meta, profile);
        } catch (Exception e) {
            System.err.println("Failed to get create profile for custom player head:");
            e.printStackTrace();
        }
        item.setItemMeta(meta);
        return item;
    }
    // 从ItemStack中提取GameProfile
    private GameProfile getGameProfileFromItemStack(ItemStack headItem) {
        if (headItem == null || !headItem.hasItemMeta()) {
            return null;
        }
        ItemMeta meta = headItem.getItemMeta();
        try {
            Field profileField = meta.getClass().getDeclaredField("profile");
            profileField.setAccessible(true);
            return (GameProfile) profileField.get(meta);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    // 放置自定义头颅方块
    private void placeCustomHead(Player player, ItemStack headItem) {
        Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
        block.setType(Material.SKULL);
        GameProfile profile = getGameProfileFromItemStack(headItem);
        if (profile == null) {
            player.sendMessage("§c无法获取头颅的皮肤信息！");
            return;
        }
        try {
            CraftWorld craftWorld = (CraftWorld) player.getWorld();
            TileEntitySkull skullTile = (TileEntitySkull) craftWorld.getHandle()
                    .getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
            skullTile.setGameProfile(profile);
            // 更新
            skullTile.update();
            block.getState().update(true, true);
        } catch (Exception e) {
            player.sendMessage("§c放置自定义头颅时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

}
